//
//  BSKTypePasswdAlertView.m
//  MicroMemory
//
//  Created by rimi on 16/7/21.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKTypePasswdAlertView.h"
#import "BSKconsts.h"
#import "BSKNoteType.h"
#import "BSKTheme.h"

@interface BSKTypePasswdAlertView () <UITextFieldDelegate>
@property(nonatomic,weak) BSKNoteType * type;
@property(nonatomic,strong) UILabel * titleLabel;
@property(nonatomic,strong) UITextField * passwdTexField;
@property(nonatomic,strong) UIView * ContentView;
@property(nonatomic,copy) void(^successCallBack)(BOOL isSuccess);
@end
@implementation BSKTypePasswdAlertView

- (instancetype)initWithNoteType:(BSKNoteType *)type successCallBack:(void(^)(BOOL isSuccess))callback
{
	self = [super init];
	if (self) {
		self.type = type;
		_successCallBack = callback;
	}
	return self;
}
-(void)loadUI{
	[super loadUI];
	[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
	
	_ContentView = [[UIView alloc]init];
	_ContentView.center = CGPointMake(ScreenWidth/2, ScreenHeight/2);
	_ContentView.backgroundColor = [UIColor whiteColor];
	_ContentView.layer.cornerRadius = 20;
	_ContentView.bounds = CGRectMake(0, 0, ScreenWidth*0.8, 150);
	_ContentView.layer.masksToBounds = YES;
	[self addSubview:_ContentView];
	_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _ContentView.bounds.size.width, 50)];
	_titleLabel.textAlignment = NSTextAlignmentCenter;
	_titleLabel.textColor = [UIColor whiteColor];
	_titleLabel.backgroundColor = [BSKTheme curentTheme].noteBackgroundColor;
	[_ContentView addSubview:_titleLabel];
	_passwdTexField = [[UITextField alloc]init];
	_passwdTexField.center = CGPointMake(_ContentView.bounds.size.width/2, 75);
	_passwdTexField.borderStyle = UITextBorderStyleRoundedRect;
	_passwdTexField.bounds = CGRectMake(0, 0, _ContentView.bounds.size.width*0.7, 30);
	_passwdTexField.placeholder = @"请输入密码";
	_passwdTexField.layer.borderWidth = 1;
	_passwdTexField.layer.cornerRadius = 10;
	_passwdTexField.layer.borderColor = [BSKTheme curentTheme].themeColor.CGColor;
	_passwdTexField.secureTextEntry = YES;
    _passwdTexField.delegate = self;
	[_ContentView addSubview:_passwdTexField];
	UIButton * okButton = [UIButton buttonWithType:UIButtonTypeCustom];
	okButton.backgroundColor =[BSKTheme curentTheme].themeColor;
	okButton.bounds = CGRectMake(0, 0,  _ContentView.bounds.size.width*0.35, 30);
	okButton.layer.cornerRadius = 10;
	okButton.center = CGPointMake(_ContentView.bounds.size.width*0.75, 125);
	[okButton setTitle:@"确定" forState:UIControlStateNormal];
	[okButton addTarget:self action:@selector(success) forControlEvents:UIControlEventTouchUpInside];
	UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
	cancelButton.backgroundColor =[BSKTheme curentTheme].themeColor;
	cancelButton.bounds = CGRectMake(0, 0,  _ContentView.bounds.size.width*0.35, 30);
	cancelButton.layer.cornerRadius = 10;
	cancelButton.center =  CGPointMake(_ContentView.bounds.size.width*0.25, 125);
	[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
	[cancelButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
	[_ContentView addSubview:okButton];
	[_ContentView addSubview:cancelButton];
}

-(void)success{
	if (_successCallBack) {
		if ([_passwdTexField.text isEqualToString:_type.password]) {
			_successCallBack(YES);
		}else{
			_successCallBack(NO);
		}
		[self hide];
	}
}

-(void)hide{
	[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
	[super hide];
}

-(void)keyBoardWillHide:(NSNotification *)anotification{

	[UIView animateWithDuration:0.25 animations:^{
		_ContentView.center = CGPointMake(ScreenWidth/2, ScreenHeight/2);
	} completion:^(BOOL finished) {
		
	}];
	
}

-(void)keyBoardWillShow:(NSNotification *)anotification{

	NSDictionary * infoDic = [anotification userInfo];
	CGSize keybordsize = [[infoDic objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
	[UIView animateWithDuration:0.25 animations:^{
		_ContentView.center = CGPointMake(ScreenWidth/2, ScreenHeight - _ContentView.bounds.size.height/2 - keybordsize.height);
	} completion:^(BOOL finished) {
		
	}];
}

#pragma mark setter

-(void)setType:(BSKNoteType *)type{
	_type = type;
	_titleLabel.text = [NSString stringWithFormat:@"分类:【%@】已加密",type.typeName];
	
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.location > 11) {
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self success];
    return YES;
}
@end
